﻿
/*=========================================
* Author: Administrator
* DateTime:2017/6/21 12:39:43
* Description:$safeprojectname$
==========================================*/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;

namespace OrderSystem
{
    public class WaiterView : MonoBehaviour
    {
        public UnityAction<WaiterItem> SendMenu = null;
        public UnityAction<WaiterItem> SendFood = null;
        public UnityAction<WaiterItem> WaiterWaitSendFodd = null;
        private ObjectPool<WaiterItemView> objectPool = null;
        private List<WaiterItemView> waiters = new List<WaiterItemView>();
        private Transform parent = null;

        private void Awake()
        {
            parent = this.transform.Find("Content");
            var prefab = Resources.Load<GameObject>("Prefabs/UI/WaiterItem");
            objectPool = new ObjectPool<WaiterItemView>(prefab , "WaiterPool");
        }
        public void UpdateWaiter( IList<WaiterItem> waitersData )

        {
            if (waitersData == null)
            {
                Debug.Log("waitersData为空");
                return;
            }
            for ( int i = 0 ; i < this.waiters.Count ; i++ )
                objectPool.Push(this.waiters[i]);

            this.waiters.AddRange(objectPool.Pop(waitersData.Count));
            
            for ( int i = 0 ; i < this.waiters.Count ; i++ )
            {
                
                if (i < waitersData.Count)

                { this.waiters[i].transform.SetParent(parent);
                    this.waiters[i].gameObject.SetActive(true);

                    WaiterItem item = waitersData[i];

                    this.waiters[i].transform.Find("Id").GetComponent<Text>().text = item.ToString();
                    Color color = Color.white;
                    if (item.state.Equals(E_WaiterState.Idle))
                        color = Color.white;
                    else if (item.state.Equals(E_WaiterState.SendMenu))
                    {
                        color = Color.blue;
                        StartCoroutine(SendMenuToClient(item,0.5f));
                       
                    }
                    else if (item.state.Equals(E_WaiterState.SendOrder))
                    {
                        color = Color.green;
                       
                    }
                    else if (item.state.Equals(E_WaiterState.WaitClientPay))
                    {
                        color = Color.cyan;

                    }

                    else if (item.state.Equals(E_WaiterState.SendFood))
                    {
                        color = Color.magenta;

                        StartCoroutine(SendFoodToClient(item,2f));                   
                    }
                    else if (item.state.Equals(E_WaiterState.WaiterClientMenu))
                    {
                        color = Color.red;
                        
                    }
                    this.waiters[i].GetComponent<Image>().color = color;
                }
            }
        }


        IEnumerator SendFoodToClient(WaiterItem waiter,float time)
        {
            Debug.Log("服务员到厨房去取餐");
            yield return new WaitForSeconds(time);
            SendFood(waiter);           
        }

        IEnumerator SendMenuToClient(WaiterItem waiter,float time)
        {
            yield return new WaitForSeconds(time);
            SendMenu(waiter);
        }


        public void Move( WaiterItem waiter , ClientItem client )
        {

        }

    }
}